<?php

/**
 * 会员中心
 */
namespace User\Controller;
use Common\Controller\MemberbaseController;
class AddressController extends MemberbaseController {


	function _initialize() {
		parent::_initialize();
			$this->db = D("Address");
			
	}
	
	
	public function index(){
		cookie("address_self",get_now_url());
		$uid=$this->u[id];
		$list=$this->db->where(array("uid"=>$uid))->select();

		$this->assign ( "seo_title",  "地址管理" );

		$this->assign ( "_list", $list );
		$this->display();
	}
	
	
	public function add(){
		
		if(IS_POST){
			

			$order=I("post.order");
			$_POST[uid]=$this->u[id];




			if ($this->db->create()) {
				
				if ($this->db->add()!==false) {


					$url= cookie("address_self") ;
					$this->success("添加成功！",$url);


				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{



			$this->assign ( "seo_title",  "添加地址" );

		$this->display();
		}
		}



	function edit(){


		if(IS_POST){
			$_POST["address"]=$_POST["shengshi"].$_POST["jiedao"];
			if ($this->db->create($_POST)) {

				if ($this->db->save()!==false) {


					$this->success("保存成功！", U("/User/Address/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}

		}else{

			$id= intval(I("get.id"));
			if(!$id){
				$this->error("缺少参数！");
			}

			$r=$this->db->where("id=$id")->find();
			$this->assign ( "seo_title",  "编辑" );
			$this->assign("data",$r);
			$this->display("");
		}
	}



	function delete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));

			if ($this->db->delete($id)) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
		if(isset($_POST['ids'])){
			$ids=I("param.ids");

			$map[id]=array("in",$ids);

			if ($this->db->where($map)->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	}


	public function changedefault() {
		$id = $_REQUEST[id];

		$uid = $this->db->where(array("id" => $id))->getField('uid');


		$this->db->where(array("uid"=>$uid))->save(array("default"=>0));


		$this->db->where(array("id"=>$id))->save(array("default"=>1));


		$this->success("操作成功！");
	}







	}
    
